package com.srs.backend.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.backend.mapper.OrderMapper;
import com.srs.backend.mapper.ServerMapper;
import com.srs.backend.pojo.Manager;
import com.srs.backend.pojo.Orders;
import com.srs.backend.pojo.Server;
import com.srs.backend.service.ServerService;
import com.srs.backend.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ServerServiceImpl implements ServerService {
    @Resource
    private ServerMapper serverMapper;
    @Resource
    private OrderMapper orderMapper;

    @Override
    public Result add(Map<String, String> data) {
        String name = data.get("name");
        String sex = data.get("sex");
        String phone = data.get("phone");
        String remark = data.get("remark");
        Integer uid;

        try {
            uid = Integer.parseInt(data.get("uid"));
        } catch (Exception e) {
            return Result.error("uid有误");
        }


        if (name == null || name.length() == 0) {
            return Result.error("服务员姓名不能为空");
        }

        if (phone.length()>30){
            return Result.error("号码长度过长");
        }

        QueryWrapper<Server> serverQueryWrapper = new QueryWrapper<>();
        serverQueryWrapper.eq("name", name);
        List<Server> servers = serverMapper.selectList(serverQueryWrapper);
        if (!servers.isEmpty()) {
            return Result.error("当前服务员已经存在");
        }
        serverMapper.insert(new Server(null,name,sex,phone,remark, uid));

        return Result.ok("success");
    }

    @Override
    public Result delete(Map<String, String> data) {
        String idStr = data.get("id");
        Integer id = null;
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
            return Result.error("id数据有误");
        }
        Server curServer = serverMapper.selectById(id);
        if (curServer == null) {
            return Result.error("当前服务员不存在或已被删除");
        }
        serverMapper.deleteById(curServer);
        return Result.ok();
    }

    @Override
    public Result modify(Map<String, String> data) {
        String idStr = data.get("id");
        Integer id = null;
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
            return Result.error("id数据有误");
        }
        Server curServer = serverMapper.selectById(id);
        if (curServer == null) {
            return Result.error("当前服务员不存在或已被删除");
        }

        String name = data.get("name");
        if (name == null || name.length() == 0) {
            return Result.error("服务员姓名不能为空");
        }

        String sex = data.get("sex");
        String phone = data.get("phone");
        if (phone.length()>30){
            return Result.error("号码长度过长");
        }

        String remark = data.get("remark");

        QueryWrapper<Server> serverQueryWrapper = new QueryWrapper<>();
        serverQueryWrapper.eq("name", name);
        List<Server> servers = serverMapper.selectList(serverQueryWrapper);
        if (!servers.isEmpty()) {
            return Result.error("当前服务员已经存在");
        }

        curServer.setName(name);
        curServer.setSex(sex);
        curServer.setPhone(phone);
        curServer.setRemark(remark);

        // 更新评价星级
        List<Integer> reviewList = getReviewListFromData(data);
        curServer.calculateServerReviewStar(reviewList);

        serverMapper.updateById(curServer);
        return Result.ok();
    }

    // 从数据集合中获取评价列表
    private List<Integer> getReviewListFromData(Map<String, String> data) {
        List<Integer> reviewList = new ArrayList<>();
        for (Map.Entry<String, String> entry : data.entrySet()) {
            if (entry.getKey().startsWith("review")) {
                try {
                    int review = Integer.parseInt(entry.getValue());
                    reviewList.add(review);
                } catch (NumberFormatException e) {
                    // 忽略无效的评价值
                }
            }
        }
        return reviewList;
    }

    @Override
    public Result getAll() {
        return Result.ok(serverMapper.selectList(null));
    }

    // 通过订单获取对应的服务员信息
    @Override
    public Result getServerByOrderId(Map<String, String> data) {
        String orderIdStr = data.get("order_id");
        Integer orderId;
        try {
            orderId = Integer.parseInt(orderIdStr);
        } catch (Exception e) {
            return Result.error("数据类型有误");
        }
        Orders orders = orderMapper.selectById(orderId);
        if (orders == null) {
            return Result.error("该订单不存在或已被删除");
        }

        Server server = serverMapper.selectById(orders.getServerId());

        if (server == null) {
            return Result.error("该订单对相应的服务员不存在");
        }

        return Result.ok(server);
    }

    @Override
    public Result getServerInfo(Map<String, String> data) {
        String uidStr = data.get("uid");
        Integer uid;
        try {
            uid = Integer.parseInt(uidStr);
        } catch (Exception e) {
            return  Result.error("uid类型有误");
        }
        QueryWrapper<Server> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        Server server = serverMapper.selectOne(queryWrapper);
        if (server == null) {
            return Result.error("该 管理员 不存在或已被删除");
        }
        return Result.ok(server);
    }
}
